// 高阶函数  函数作为参数

function myForEach(arr, fn) {
  for (let i = 0; i < arr.length; i++) {
    fn(arr[i], i);
  }
}

let arr = [1, 2, 3, 6, 7, 8];

myForEach(arr, (item, index) => {
  console.log('myForEach==>', item, index);
});

function myMap(arr, fn) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    res.push(fn(arr[i], i));
  }
  return res;
}

const temp = myMap(arr, (item, index) => item % 2 === 0);
console.log('myMap==>', temp);

function myEvery(arr, fn) {
  let ans = true;
  for (let i = 0; i < arr.length; i++) {
    // ans = !!fn(arr[i], i);
    ans = Boolean(fn(arr[i], i));
    if (!ans) {
      break;
    }
  }
  return ans;
}

const flag1 = myEvery(arr, (item, index) => item % 2 === 0);
console.log('myEvery==>', flag1);

function mySome(arr, fn) {
  let ans = false;
  for (let i = 0; i < arr.length; i++) {
    // ans = !!fn(arr[i], i);
    ans = Boolean(fn(arr[i], i));
    if (ans) {
      break;
    }
  }
  return ans;
}

const flag2 = mySome(arr, (item, index) => item > 6);
console.log('mySome==>', flag2);
